package com.yicong.ants.ui.me.recommend;

import android.graphics.Rect;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.util.StringHelper;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.me.ChildrenResp;
import com.yicong.ants.databinding.RecyclerBinding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 已推荐的好友
 *
 * @author cchao
 * @version 2019-05-28.
 */
public class MyDirectListActivity extends BaseTitleBarActivity<RecyclerBinding> {
    int mIsDirect = 0;
    String mType = "0";

    @Override
    protected int getLayout() {
        return R.layout.recycler;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("直接邀请");
        mIsDirect = getIntent().getIntExtra(Constants.Extra.IsDirect, 1);
        // 筛选
        addTitleMenuItem(R.drawable.title_menu_split, v -> {
            String[] menuText = {"团队总人数排序", "创建时间排序", "邀请贡献值排序", "活跃度排序"};
            String[] ids =        {"0", "10", "20", "30"};
            UiHelper.showItemsDialog(mContext, "排序选择", menuText, (dialog, which) -> {
                mType = ids[which];
                onLoadData();
            });
        });
        initAdapter();
        onLoadData();
    }

    StatefulBindQuickAdapter<ChildrenResp.ItemBean> mAdapter;

    private void initAdapter() {
        mDataBind.recycler.setBackgroundColor(UiHelper.getColor(R.color.f4f4f4));
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<ChildrenResp.ItemBean>(R.layout.children_direct_item, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindViewHolder helper, ChildrenResp.ItemBean item) {
                helper.getBinding().setVariable(BR.bean, item);
                View root = helper.getView(R.id.root);
                if (StringHelper.isNotEmpty(item.getLeave_task_text())) {
                    root.getLayoutParams().height = UiHelper.dp2px(136);
                } else {
                    root.getLayoutParams().height = UiHelper.dp2px(113);
                }
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
        mDataBind.recycler.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                int adapterPosition = parent.getChildAdapterPosition(view);
                outRect.bottom = UiHelper.dp2px(12);
                outRect.top = UiHelper.dp2px(0);
                if (adapterPosition == 0) {
                    outRect.top = UiHelper.dp2px(12);
                }
            }
        });
    }

    private void loadPage(final int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));
        params.put("is_direct", String.valueOf(mIsDirect));
        params.put("sort", String.valueOf(mType));

        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }

        addSubscribe(RetrofitHelper.getApis().directChildren(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    AdapterHelper.solveFailResp(mDataBind.recycler, page, respBean);
                    return;
                }
                // 为空
                if (CollectionHelper.isEmpty(respBean.getData().getList())) {
                    mAdapter.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                    return;
                }

                List<ChildrenResp.ItemBean> data = respBean.getData().getList();
                mAdapter.solvePageData(data, page, respBean.getData().getPageInfo().getCountPage());
            }, RxHelper.getSwitchableErrorConsumer(mAdapter)));
    }

    @Override
    protected void onLoadData() {
        loadPage(1);
    }
}
